package ru.evga.inse.server.gwt.user.client.menu.general;

import ru.evga.gwt.lib.common.client.request.IServiceRequestCallback;
import ru.evga.gwt.lib.common.client.request.RequestFactory;
import ru.evga.gwt.lib.common.client.request.params.SaveEntityParams;
import ru.evga.inse.server.gwt.common.client.domain.dict.CityDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;


/**
 * The outermost UI of the application.
 */
public class GeneralContentView
    extends Composite
{
    interface Binder
        extends UiBinder<Widget, GeneralContentView>
    {
    }

    public interface Presenter
    {
        void goToRealty();

        void goToForum();
    }
    private static final Binder BINDER = GWT.create(Binder.class);
    Presenter m_presenter;
    @UiField
    Button m_testButton;
    
    @UiField
    TextBox m_entity;
    @UiField
    TextBox m_id;
    
    @UiField
    Label idLabel;
    @UiField
    Label nameLabel;
    @UiField
    Label errorLabel;
    

    public GeneralContentView()
    {
        initWidget(BINDER.createAndBindUi(this));
        m_testButton.addClickHandler(new ClickHandler()
        {
            public void onClick(ClickEvent event)
            {

                errorLabel.setText("");
                SaveEntityParams<CityDto> params = SaveEntityParams.createEmptyParams();
                CityDto cityDto = CityDto.createEmptyCityDto();
                cityDto.setName(m_entity.getText());
                params.setEntityName(cityDto.getEntityName());
                params.setEntity(cityDto);
                RequestFactory.saveEntity(params, new IServiceRequestCallback<CityDto, String>()
                    {

                        public void ok(CityDto entity)
                        {
                            idLabel.setText(""+entity.getId());
                            nameLabel.setText(entity.getName());
                        }

                        public void error(String data)
                        {
                            errorLabel.setText(data);
                        }
                    });
                }


//                FindEntityParams params = FindEntityParams.createEmptyParams(); 
//                params.setEntityName("city");
//                params.setId(Integer.decode(m_id.getText()));
//                RequestFactory.findEntity(params, new IFindCallback<CityDto>()
//                    {
//
//                        public void ok(CityDto entity)
//                        {
//                            idLabel.setText(""+entity.getId());
//                            nameLabel.setText(entity.getName());
//                        }
//
//                        public void error(String data)
//                        {
//                            errorLabel.setText(data);
//                        }
//                    });
//                }


//                FindEntityRangeParams params = FindEntityRangeParams.createEmptyParams(); 
//                params.setAsc(true);
//                params.setEntityName("city");
//                params.setFirstResult(Integer.decode(m_id.getText()));
//                params.setMaxResults(10);
//                params.setOrderBy("name");
//                RequestFactory.findEntityRange(params, new IFindCallback<ResultContainer<CityDto>>()
//                    {
//
//                        public void ok(ResultContainer<CityDto> entities)
//                        {
//                            idLabel.setText(""+entities.getResult().get(0).getId());
//                            nameLabel.setText(entities.getResult().get(0).getName());
//                        }
//
//                        public void error(String data)
//                        {
//                            errorLabel.setText(data);
//                        }
//                    });
//                }

        });
    }

    public void setPresenter(Presenter presenter)
    {
        m_presenter = presenter;
    }
}
